﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Web.Configuration;

namespace AppVulneravel.Models
{
    public class Usuario
    {
        public int Id { get; set; }
        public string Nome { get; set; }
        public string Login { get; set; }
        public string Senha { get; set; }
        public string Email { get; set; }

        public Usuario()
        {
        }
        public Usuario(int pId
                     , string pNome
                     , string pLogin
                     , string pSenha
                     , string pEmail
                  )
        {
            Id = pId;
            Nome = pNome;
            Login = pLogin;
            Senha = pSenha;
            Email = pEmail;
        }

        public void Carregar(int id)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select ");
            sql.Append("Id");
            sql.Append(",Nome");
            sql.Append(",Login");
            sql.Append(",Senha");
            sql.Append(",Email");
            sql.Append(" from  Usuario");
            sql.Append(" WHERE Id = " + id);

            var connString = WebConfigurationManager.ConnectionStrings["TCC"].ConnectionString;
            using (var conn = new SqlConnection(connString))
            {
                var command = new SqlCommand(sql.ToString(), conn);
                command.Connection.Open();
                List<Usuario> u = new List<Usuario>();

                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    this.Id = Convert.ToInt32(reader.GetValue(0).ToString());
                    this.Nome = reader.GetValue(1).ToString();
                    this.Login = reader.GetValue(2).ToString();
                    this.Senha = reader.GetValue(3).ToString();
                    this.Email = reader.GetValue(4).ToString();
                }
            }
        }

        public bool CarregarLogin(string Login, string Senha)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("SELECT ");
            sql.Append("id");
            sql.Append(",nome");
            sql.Append(",login");
            sql.Append(",senha");
            sql.Append(",email");
            sql.Append(" FROM  usuario");
            sql.Append(" WHERE login = '" + Login);
            sql.Append("' AND senha = '" + Senha + "'");

            var connString = WebConfigurationManager.ConnectionStrings["TCC"].ConnectionString;
            using (var conn = new SqlConnection(connString))
            {
                var command = new SqlCommand(sql.ToString(), conn);
                command.Connection.Open();
                List<Usuario> u = new List<Usuario>();

                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    this.Id = Convert.ToInt32(reader.GetValue(0).ToString());
                    this.Nome = reader.GetValue(1).ToString();
                    this.Login = reader.GetValue(2).ToString();
                    this.Senha = reader.GetValue(3).ToString();
                    this.Email = reader.GetValue(4).ToString();
                    return true;
                }
            }
            return false;
        }
    }
}